package tgc.edu.demo.jkxt.controller;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import tgc.edu.demo.jkxt.custom.AjaxResult;
import tgc.edu.demo.jkxt.custom.BaseController;
import tgc.edu.demo.jkxt.entity.Course;
import tgc.edu.demo.jkxt.entity.Grade;
import tgc.edu.demo.jkxt.entity.Shijuan;
import tgc.edu.demo.jkxt.entity.Student;
import tgc.edu.demo.jkxt.entity.SysRole;
import tgc.edu.demo.jkxt.form.GradeForm;
import tgc.edu.demo.jkxt.security.UserUtils;
import tgc.edu.demo.jkxt.service.CourseService;
import tgc.edu.demo.jkxt.service.GradeService;
import tgc.edu.demo.jkxt.service.ShijuanService;
import tgc.edu.demo.jkxt.service.StudentService;
import tgc.edu.demo.jkxt.service.TbClassService;



@RestController
@RequestMapping(value = "/Grade")
@CrossOrigin
public class GradeController extends BaseController<Grade, Integer,GradeForm>{
	
	@Autowired
	private GradeService gradeService;
	@Autowired
	private UserUtils userUtils;
	@Autowired
	private TbClassService tbclassService;
	@Autowired
	private CourseService courseService;
	@Autowired
	private StudentService studentService;
	@Autowired
	private ShijuanService shijuanService;
	
	
	@RequestMapping(value = "/bjChange")
	@ResponseBody
	public Object bjChange(Integer bjId,ModelMap modelMap) {
		List<Course> courses=gradeService.findCourseByTbClassId(bjId);
		List<List<Object>> cjb= gradeService.getCjb(bjId,courses);
		modelMap.put("kcs", courses);
		modelMap.put("cjb", cjb);
		System.out.println(cjb);
		return cjb;
	}
	@RequestMapping(value = "/bjChange2")
	@ResponseBody
	public Object bjChange2(Integer bjId,ModelMap modelMap) {
		List<Course> courses=gradeService.findCourseByTbClassId(bjId);
		List<List<Object>> cjb= gradeService.getCjb(bjId,courses);
		modelMap.put("kcs", courses);
		modelMap.put("cjb", cjb);
		System.out.println(cjb);
		return courses;
	}

	@RequestMapping(value = "/save1")
	public Object save0(GradeForm grade) {
		try {
		SysRole sysRoles = new SysRole(3);
		if(userUtils.getUser().getRoles().equals(sysRoles)) {
		Student student = studentService.findById(userUtils.getUser().getId());
		Shijuan shijuan = shijuanService.findById(grade.getShijuanId());
		Course course = shijuan.getCoures();
		Integer id = grade.getId();
		Grade model=new Grade();
		if(id!=null) {
			model=gradeService.findById(id);
		}
		model.setCourse(course);
		model.setShijuan(shijuan);
		model.setStudent(student);
		model.setGrade(grade.getGrade());
		BeanUtils.copyProperties(grade, model, "id");
		gradeService.save(model);
		return new AjaxResult(model);
		}else {
			return new AjaxResult("只有学生能考");
		}
		} catch (Exception e) {
			e.printStackTrace();
			return new AjaxResult(false, "数据保存失败");
		}
	}
	@RequestMapping(value = "/find")
	public Object FindByShijuanIdAndStudentId(Integer shijuanId) {
	Integer studentId=	userUtils.getUser().getId();
	if(gradeService.findByShijuanIdAndStudentId(shijuanId,studentId)!=null) {
		Grade grade=gradeService.findByShijuanIdAndStudentId(shijuanId,studentId);		
		return new AjaxResult(grade);
	}else {
			return new AjaxResult(false,"可以考试！");
	}
	}
	
}
